import axios from 'axios'
let host = process.env.NODE_ENV !== 'production'?'http://172.16.8.45:8044' : 'http://fdapidb.fastfish.com:8069';
//请求失败
function errorState(error) {
    if (error.response &&  error.response.status === 400) {
        return error.response
    } else if(error.response && (error.response.status === 403)) {
        return error.response
    } else {
        return new Error('接口报错了');
    }
}

// 请求成功
function successState(res, noMessage) {
    if(res.data.code != 1){
        /*store.commit('dialogErr', {
            message: res.data.message,
            noMessage
        });*/
    }
}

// 配置axios实例参数
const httpServer = (opts) => {
    let httpDefaultOpts = { //http默认配置
        method: opts.method,
        url: host + opts.url,
        timeout: 1800000, //前端30min延时控制
        params: opts.params,
        data: opts.data,
        headers: opts.type == 'formdata' ? {
            "Content-Type": "multipart/form-data"
        } : {

        }
    }
    if(opts.method == 'get' || opts.method=='GET'){
        delete httpDefaultOpts.data
    }else{
        delete httpDefaultOpts.params
    }
    // 创建axios实例
    let promise = new Promise(function(resolve, reject) {
        axios(httpDefaultOpts).then(
            (res) => {
                successState(res, opts.noMessage)
                resolve(res)
            }
        ).catch(
            (err) => {
                errorState(err, httpDefaultOpts.router);
                reject(err)
            }
        )

    })
    return promise
}

export default httpServer
